//
//  WBVisitorView.swift
//  weibo
//
//  Created by iobit on 2017/06/21.
//  Copyright © 2017 zhuxu. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {

    /// 使用字典设置访客信息
    /// - Parameter dict: imageName，message
    ///注：首页的imageName == ""
    var visitorInfo:[String:String]? {
        didSet {
            guard let imageName = visitorInfo?["imageName"],
                let message = visitorInfo?["message"] else {
                    return
            }
            
            tipLabel.text = message
            //首页已经设置好了，不需要设置
            if imageName == "" {
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            houseIconView.isHidden = true//其他页面隐藏
            maskIconView.isHidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //图像视图
    lazy var iconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
   
    //遮罩视图
    lazy var maskIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //小房子
    lazy var houseIconView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //提示label
    lazy var tipLabel = UILabel()
   
    //注册按钮
    lazy var registerButton = UIButton()

    //登录按钮
    lazy var loginButton = UIButton()
}

// MARK: - 设置界面
extension WBVisitorView {

    func setupUI() {
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)//遮罩图下方颜色为0XEDEDED
        //设置标签label的属性
        tipLabel.text = "关注一些人，回这里看看有什么惊喜"
        tipLabel.font = UIFont.systemFont(ofSize: 14)
        tipLabel.textColor = UIColor.darkGray
        tipLabel.numberOfLines = 0
        tipLabel.sizeToFit()
        tipLabel.textAlignment = .center
        
        //设置注册按钮属性
        registerButton.setTitle("注册", for: .normal)
        registerButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        registerButton.setTitleColor(UIColor.orange, for: .normal)
        registerButton.setTitleColor(UIColor.black, for: .highlighted)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        registerButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.highlighted)
        registerButton.sizeToFit()
        
        //设置登录按钮属性
        loginButton.setTitle("登录", for: .normal)
        loginButton.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        loginButton.setTitleColor(UIColor.darkGray, for: .normal)
        loginButton.setTitleColor(UIColor.black, for: .highlighted)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.normal)
        loginButton.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: UIControlState.highlighted)
        loginButton.sizeToFit()
        
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseIconView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //原生自动布局
        //图像视图
        addConstraint(.init(item: iconView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: iconView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -60))

        //小房子
        addConstraint(.init(item: houseIconView, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: houseIconView, attribute: .centerY, relatedBy: .equal, toItem: iconView, attribute: .centerY, multiplier: 1.0, constant: 0))


        
        //提示label
        addConstraint(.init(item: tipLabel, attribute: .centerX, relatedBy: .equal, toItem: iconView, attribute: .centerX, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: tipLabel, attribute: .top, relatedBy: .equal, toItem: iconView, attribute: .bottom, multiplier: 1.0, constant: 20.0))
        addConstraint(.init(item: tipLabel, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
        
        //注册按钮
        addConstraint(.init(item: registerButton, attribute: .left, relatedBy: .equal, toItem: tipLabel, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: registerButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20.0))
        addConstraint(.init(item: registerButton, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 100))
        
        //登录按钮
        addConstraint(.init(item: loginButton, attribute: .right, relatedBy: .equal, toItem: tipLabel, attribute: .right, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: loginButton, attribute: .top, relatedBy: .equal, toItem: tipLabel, attribute: .bottom, multiplier: 1.0, constant: 20.0))
        addConstraint(.init(item: loginButton, attribute: .width, relatedBy: .equal, toItem: registerButton, attribute: .width, multiplier: 1.0, constant: 0))
        
        //遮罩视图
        addConstraint(.init(item: maskIconView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: maskIconView, attribute: .bottom, relatedBy: .equal, toItem: registerButton, attribute: .bottom, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: maskIconView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0))
        addConstraint(.init(item: maskIconView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0))
    }
    
    func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2*Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        //动画完成不删除，如果iconView被释放，动画属性一起被销毁
        anim.isRemovedOnCompletion = false
        iconView.layer.add(anim, forKey:nil)
        
    }
    
}
